package com.avicit.zipkinkafkaesserver.service;

import com.avicit.zipkinkafkaesserver.dao.ZipkinUpdateMsgDao;
import com.avicit.zipkinkafkaesserver.entity.ZipkinUpdateMsg;
import com.avicit.zipkinkafkaesserver.factory.IDFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 记录定时获取es 数据情况
 */
@Service
public class ZipkinUpdateMsgService {

    @Autowired
    private ZipkinUpdateMsgDao zipkinUpdateMsgDao;


    public List<ZipkinUpdateMsg> findList() {
        return (List<ZipkinUpdateMsg>) zipkinUpdateMsgDao.findAll();
    }

//    public List<ZipkinUpdateMsg> findByJobType(String jobType) {
//        return zipkinUpdateMsgDao.findByJobType(jobType,Sort.Order.desc("esStartTime"));
//    }

//    public ZipkinUpdateMsg findLast(String jobType) {
//        PageRequest pageRequest =  PageRequest.of(0,1, Sort.Direction.DESC,"esStartTime");
//        Page<ZipkinUpdateMsg> all = zipkinUpdateMsgDao.findByJobType(jobType,pageRequest);
//        ZipkinUpdateMsg zipkinUpdateMsg = all.get().collect(Collectors.toList()).get(0);
//        return zipkinUpdateMsg;
//    }

    public void update(String id,ZipkinUpdateMsg zipkinUpdateMsg) {
//        Optional<ZipkinUpdateMsg> byId = zipkinUpdateMsgDao.findById(id);
//        ZipkinUpdateMsg zipkinUpdateMsg1 = byId.get();
//        BeanUtils.copyProperties(zipkinUpdateMsg, zipkinUpdateMsg1);
//        zipkinUpdateMsgDao.save(zipkinUpdateMsg1);
    }

    public void save(ZipkinUpdateMsg build) {
        build.setId(IDFactory.uuid());
        zipkinUpdateMsgDao.save(build);
    }

    public ZipkinUpdateMsg findByQueryDateAndUpdateStatus(String queryDate, String jobType, boolean updateStatus) {
        return zipkinUpdateMsgDao.findByQueryDateAndUpdateStatusAndJobType(queryDate,updateStatus, jobType);
    }
}
